#!/bin/bash

echo "Executing script by: lichangsheng 24055060132"
echo "-----------------------------------------"

SOURCE_FILE="scores.txt"
OUTPUT_DIR="students"

# 检查源文件是否存在
if [ ! -f "$SOURCE_FILE" ]; then
    echo "Error: Source file '$SOURCE_FILE' not found!"
    exit 1
fi

# 创建输出目录
mkdir -p "$OUTPUT_DIR"

# 循环读取scores.txt内容
while read name gender id java_subj java_score python_subj python_score cpp_subj cpp_score linux_subj linux_score; do
    
    # 计算总分
    total=$(( java_score + python_score + cpp_score + linux_score ))
    
    # 计算平均分（保留1位小数）
    average=$(echo "scale=1; $total / 4" | bc)
    
    # 定义学生文件名
    student_file="$OUTPUT_DIR/$name.txt"
    
    # 写入学生信息到文件
    cat > "$student_file" << EOF
name: $name
id: $id
gender: $gender
scores: java $java_score python $python_score c++ $cpp_score linux $linux_score
total: $total
average: $average
EOF

done < "$SOURCE_FILE"

echo "-----------------------------------------"
echo "All student files have been created in the '$OUTPUT_DIR' directory."
exit 0
